package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import chat.ChatConsumer;
import client.ClientUser;

import com.sun.media.jsdt.Channel;
import com.sun.media.jsdt.ConnectionException;
import com.sun.media.jsdt.Data;
import com.sun.media.jsdt.JSDTException;
import com.sun.media.jsdt.TimedOutException;

public class ChatView extends JPanel implements ActionListener {
	private Label sayLabel;
	private TextArea messageArea;
	private TextArea clientArea;
	private String user;
	private ClientUser clientUser;
	private TextField textField;
	private Data data;
	private JComboBox listClients;

	public ChatView(ClientUser clientUser) {
		this.user = clientUser.getClient().getName();
		this.clientUser = clientUser;
		this.setPreferredSize(new Dimension(340, 340));
		this.setLayout(new BorderLayout());
		this.initialize();
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	private ChatConsumer chatConsumer;

	public ChatConsumer getChatConsumer() {
		return chatConsumer;
	}

	public void setChatConsumer(ChatConsumer chatConsumer) {
		this.chatConsumer = chatConsumer;
		this.initialize();
	}

	@SuppressWarnings("deprecation")
	public void initialize() {
		this.setPreferredSize(new Dimension(300, 300));
		this.setBorder(BorderFactory.createTitledBorder("Chat"));

		Font lbl = new Font("Helvetica", Font.BOLD, 14);
		
		DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
		
		listClients = new JComboBox();
		listClients.setModel(comboBoxModel);
		listClients.addItem("--Tous--");

		Panel msgPanel = new Panel();
		//msgPanel.add(new JLabel(new ImageIcon( "contact_icon.jpg")));
		msgPanel.add(new JLabel(new ImageIcon(getClass().getResource("/images/contact_icon.jpg"))));
		msgPanel.add(listClients);
		
		messageArea = new TextArea(22, 25);
        JScrollPane scrollPane = new JScrollPane(messageArea);
        msgPanel.add(scrollPane);
        
		messageArea.setEditable(false);
		this.add("Center", msgPanel);

		Panel sayPanel = new Panel();
		JLabel chatIcon = new JLabel(new ImageIcon(getClass().getResource("/images/Chat-icon.png")));
		JButton sendButton = new JButton("Envoyer");
		sayPanel.add(chatIcon);
		sayPanel.add(textField = new TextField(15));
		sayPanel.add(sendButton);
		
		textField.addActionListener(this);

		sendButton.addActionListener(this);

		this.add("South", sayPanel);
	

		setVisible(true);

		this.chatConsumer = new ChatConsumer(clientUser.getClient().getName(),
				messageArea);
		try {
			clientUser.getChannelChat().addConsumer(clientUser.getClient(),
					chatConsumer);
		} catch (JSDTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void initListClients(String[] listClientNames, String user){
		for(int i=0; i<listClientNames.length ; i++){
			if(!listClientNames[i].equals(user)){
				addNewClient(listClientNames[i]);
			}
		}
	}

	public void addNewClient(String client) {
		listClients.addItem(client);
		updateView();
		System.out.println("ChatView :: addItem : " + client);
	}

	public void updateView() {
		this.repaint();
	}

	public String[] listeClientNameDansSession() {
		String[] listeNames = null;
		try {
			listeNames = clientUser.getSession().listClientNames();
		} catch (JSDTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listeNames;
	}

	public void writeLine(String msg) {
		try {
			data = new Data(msg);
			data.setPriority(Channel.HIGH_PRIORITY);
			if(listClients.getSelectedItem().equals("--Tous--")){
				clientUser.getChannelChat().sendToAll(clientUser.getClient(), data);
				
			}else{
				clientUser.getChannelChat().sendToClient(clientUser.getClient(),clientUser.getClient().getName(),data);
				clientUser.getChannelChat().sendToClient(clientUser.getClient(),(String) listClients.getSelectedItem(),data);
			}
			this.textField.setText(null);

		} catch (ConnectionException ce) {
			System.out.println("ChatUser: writeLine: exception: " + ce);
			System.out.println("ChatUser: writeLine: disconnecting...");
			// disconnect();
		} catch (TimedOutException ce) {
			System.out.println("ChatUser: writeLine: exception: " + ce);
			System.out.println("ChatUser: writeLine: disconnecting...");
			// disconnect();
		} catch (Exception e) {
			System.err.print("Caught exception in ");
			System.err.println("ChatUser.writeLine(): " + e);
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (clientUser.getSession() != null) {
			try {
				writeLine(textField.getText());
			} catch (Exception e) {
				System.err.println("Exception while invoking Say: " + e);
			}
		}

	}
	
}
